package com.ysd.lis.service.eq.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.EqEquipment;
import com.ysd.lis.entity.eq.EqRepairPlan;
import com.ysd.lis.entity.eq.EqRepairPlanPerson;
import com.ysd.lis.mapper.eq.EqRepairPlanMapper;
import com.ysd.lis.mapper.eq.EqRepairPlanPersonMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.eq.EqRepairDto;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.eq.EqRepairPlanPersonService;
import com.ysd.lis.service.eq.EqRepairPlanService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备管理-设备类型 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-08-16
 */
@Service("eqRepairPlanService")
public class EqRepairPlanServiceImpl extends ServiceImpl<EqRepairPlanMapper, EqRepairPlan> implements EqRepairPlanService {
    @Autowired
    EqRepairPlanMapper eqRepairPlanMapper;
    @Autowired
    EqRepairPlanPersonMapper eqRepairPlanPersonMapper;
    @Autowired
    EqRepairPlanPersonService eqRepairPlanPersonService;
    @Autowired
    SysRuleSetService sysRuleSetService;
    @Override
    public Result findRepairPlanPageList(CommonPageDto pageParam) {
        Page<EqRepairPlan> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        MPJLambdaWrapper<EqRepairPlan> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(EqRepairPlan.class)
                .selectAs("eq", EqEquipment::getEquiptName,EqRepairPlan::getEquiptName)
                .selectAs("eq",EqEquipment::getSpecification,EqRepairPlan::getSpecification)
                .leftJoin(EqEquipment.class,"eq",EqEquipment::getId,EqRepairPlan::getEquiptId)
                .eq(EqRepairPlan::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqRepairPlan::getPlanNo, pageParam.getSeachValue())
                    .or().like(EqRepairPlan::getEquiptCode, pageParam.getSeachValue()));
        }
        if(ToolsUtils.isNotEmpty(pageParam.getStatus())){
            lambdaQueryWrapper.eq(EqRepairPlan::getPlanStatus,pageParam.getStatus());
        }
        lambdaQueryWrapper.orderByDesc(EqRepairPlan::getCreateTime);
        Page<EqRepairPlan> planPage = eqRepairPlanMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", planPage);
    }

    @Override
    public Result addRepairPlan(EqRepairPlan eqRepairPlan) {
        String a = sysRuleSetService.createRuleCodeList("eqRepairPlanNo", 1).get(0);
        eqRepairPlan.setPlanNo(a);
        boolean save = this.save(eqRepairPlan);
        if (save) {
            //person
            List<EqRepairPlanPerson> repairPlanPersonList = eqRepairPlan.getRepairPlanPersonList();
            for(EqRepairPlanPerson repairPlanPerson : repairPlanPersonList){
                repairPlanPerson.setPlanId(eqRepairPlan.getId());
                repairPlanPerson.setPlanNo(eqRepairPlan.getPlanNo());
            }
            boolean b = eqRepairPlanPersonService.saveBatch(repairPlanPersonList);

            return Result.succ(1, "添加成功", eqRepairPlan);

        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result updateRepairPlan(EqRepairPlan eqRepairPlan) {
        boolean save = this.updateById(eqRepairPlan);
        if (save) {
            //person
            //先删除 再添加或修改
            LambdaQueryWrapper<EqRepairPlanPerson> delPersonWrapper = new LambdaQueryWrapper<>();
            delPersonWrapper.eq(EqRepairPlanPerson::getPlanId,eqRepairPlan.getId());
            eqRepairPlanPersonMapper.delete(delPersonWrapper);
            List<EqRepairPlanPerson> eqRepairPlanPersonList = eqRepairPlan.getRepairPlanPersonList();
            boolean b = eqRepairPlanPersonService.saveBatch(eqRepairPlanPersonList);

            return Result.succ(1, "编辑成功", eqRepairPlan);

        }else {
            return Result.fail("编辑失败");
        }
    }

    @Override
    public Result batchDeleteRepairPlanList(EqRepairDto dto) {
        List<EqRepairPlan> eqRepairPlanList = dto.getEqRepairPlanList();
        List<String> ids = eqRepairPlanList.stream().map(EqRepairPlan::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            //person
            LambdaUpdateWrapper<EqRepairPlanPerson> deleteTableWrapper = new LambdaUpdateWrapper<>();
            deleteTableWrapper.in(EqRepairPlanPerson::getPlanId, ids);
            eqRepairPlanPersonMapper.delete(deleteTableWrapper);
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result findEqRepairPlanById(EqRepairPlan eqRepairPlan) {
        String id = eqRepairPlan.getId();

        MPJLambdaWrapper<EqRepairPlan> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(EqRepairPlan.class)
                .selectAs("eq",EqEquipment::getEquiptName,EqRepairPlan::getEquiptName)
                .selectAs("eq",EqEquipment::getSpecification,EqRepairPlan::getSpecification)
                .leftJoin(EqEquipment.class,"eq",EqEquipment::getId,EqRepairPlan::getEquiptId)
                .eq(EqRepairPlan::getId,id)
                .eq(EqRepairPlan::getDelFlag, 0);
        EqRepairPlan repairPlan = eqRepairPlanMapper.selectOne(lambdaQueryWrapper);

        //获取person
        MPJLambdaWrapper<EqRepairPlanPerson> queryTableWrapper = new MPJLambdaWrapper<>();
        queryTableWrapper.selectAll(EqRepairPlanPerson.class)
                .eq(EqRepairPlanPerson::getPlanId,id)
                .eq(EqRepairPlanPerson::getDelFlag,0);
        List<EqRepairPlanPerson> personList = eqRepairPlanPersonMapper.selectList(queryTableWrapper);
        repairPlan.setRepairPlanPersonList(personList);

        return Result.succ(1, "查询成功", repairPlan);
    }
}
